package com.example.demo.controller;

import com.example.demo.jwt.JwtAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.Map;

@RestController
public class TestController {
    @Autowired
    JwtAuthService jwtAuthService;

    @PostMapping("/test")
    public String test(@RequestBody Map<String,String> map) throws Exception {
        String username = map.get("username");
        String password = map.get("password");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return "用户密码不能为空";
        }
        return jwtAuthService.login(username,password);
    }

    @GetMapping("/a")
    @Secured("ROLE_abc")
    public String a() {
        return "hello a api";
    }

    @GetMapping("/b")
    @PreAuthorize("hasRole('abcc')")
    public String b() {
        return "hello b api";
    }
}
